<?php
class DealCommentDABase extends AbstractDbObject
{
	/**
	 * Table name
	 *
	 * @var string
	 */
	protected $tableName = 'deal_comment';

	/**
	 * Table alias
	 *
	 * @var string
	 */
	protected $tableAlias = 'deal_comment';

	/**
	 * Create a product
	 *
	 * @access public
	 *
	 * @param DealCommentEntity $dealCommentEntity: object
	 *
	 * @return primary key has just been inserted
	 */
	public function create($dealCommentEntity)
	{
		try {
			// Get db object
			$dbWriter = $this->getDbWriter ();

			// Fetch array
			$data = array(
				'user_id' => $dealCommentEntity->user_id,
				'deal_id' => $dealCommentEntity->deal_id,
				'comment_text' => $dealCommentEntity->comment_text,
				'creation_date' => $dealCommentEntity->creation_date,
				'default' => $dealCommentEntity->default
			);

			// Insert into database
			$dbWriter->insert($this->tableName, $data);

			// Return primary key has just created
			return $dbWriter->lastInsertId($this->tableName, 'comment_id');
		} catch(Exception $ex) {
			throw $ex;
		}
	}

	/**
	 * Update a product
	 *
	 * @access public
	 *
	 * @param DealCommentEntity $dealCommentEntity: object
	 *
	 * @return bool true if update success otherwise return false
	 */
	public function update($dealCommentEntity)
	{
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();

			// Fetch array
			$data = array(
				'comment_id' => $dealCommentEntity->comment_id,
				'user_id' => $dealCommentEntity->user_id,
				'deal_id' => $dealCommentEntity->deal_id,
				'comment_text' => $dealCommentEntity->comment_text,
				'creation_date' => $dealCommentEntity->creation_date,
				'default' => $dealCommentEntity->default
			);

			// Update product
			$idQoute = $dbWriter->quote($dealCommentEntity->comment_id);
			$where[] = "comment_id = $idQoute";

			// Execute query
			$numRows = $dbWriter->update($this->tableName, $data, $where);
			if($numRows > 0){
				return true;
			}else{
				return false;
			}
		} catch(Exception $ex) {
			throw $ex;
		}
	}

	/**
	 * Delete a comment by Id
	 *
	 * @access public
	 *
	 * @param int $id: Id of comment
	 *
	 * @return bool whether delete action is success or not
	 */
	public function del($id)
	{
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();

			// Update product
			$idQuote = $dbWriter->quote($id);

			// Execute query
			$rowCount = $dbWriter->delete($this->tableName, "comment_id = $idQuote");

			// Return boolean result
			if($rowCount > 0){
				return true;
			}else{
				return false;
			}
		} catch(Exception $ex) {
			throw $ex;
		}
	}

	/**
	 * Get DealCommentEntity object by Id of the comment
	 *
	 * @access public
	 *
	 * @param int $id: Id of comment
	 *
	 * @return DealCommentEntity object
	 */
	public function getById ($id)
	{
		// Get db object
		$dbWriter = $this->getDbWriter();

		// Add quote
		$idQuote = $dbWriter->quote($id);

		// Query for get comment by Id
		$query = "
			SELECT {$this->tableName}.*
			FROM {$this->tableName}
		";
		$result = $dbWriter->fetchRow($query);
		if (is_null($result) || !is_array($result)){

			// Id of the product does not exist
			return null;
		}
		$dealCommentEntity = new DealCommentEntity($result);

		// Return DealCommentEntity object
		return $dealCommentEntity;
	}

	/**
	 * Get all rows of product table with search criterias
	 *
	 * @access public
	 *
	 * @param array $arrElement: array of search criterias
	 * @param string $orderBy: Field and the way ordered (ASC or DESC)
	 * @param int $begin: Begin item
	 * @param int $limit: Number rows to query
	 *
	 * @return array of DealCommentEntity object
	 */
	public function getItems($arrElement, $orderBy = null, $begin = null, $limit = null)
	{
		// Get db object
		$dbWriter = $this->getDbWriter();

		// Query for get all items of product with search criterias
		$query = "
			SELECT {$this->tableName}.*
			FROM {$this->tableName}";

		// Add where
		$query .= $this->buildWhereClause($arrElement, $dbWriter, $this->tableName);
		
		// Add order By
		$query .= $this->buildOrderClause($orderBy);

		// Add begin and Limit
		$query .= $this->buildLimitClause($begin, $limit);

		// Execute query
		$result = $dbWriter->fetchAll($query);

		$buffer = null;
		foreach ($result as $element) {
			$dealCommentEntity = new DealCommentEntity($element);

			// Add DealCommentEntity into result
			$buffer[] = $dealCommentEntity;
		}
		return $buffer;
	}
	
	public function getListComment($deal_id)
	{
		// Get db object
		$dbWriter = $this->getDbWriter();

		// Query for get all items of product with search criterias
		$query = "
			SELECT DC.*, U.*, DC.creation_date as date_post
			FROM {$this->tableName} as DC
			JOIN user as U ON U.id = DC.user_id
			WHERE DC.deal_id = {$deal_id}
			ORDER BY DC.creation_date DESC
			";
		// Execute query
		$result = $dbWriter->fetchAll($query);
		return  $result;
	}
	
	/**
	 * Get total rows of product
	 *
	 * @access public
	 *
	 * @param array $arrElement: array of search criterias
	 *
	 * @return int total: total rows of product
	 */
	public function getTotals($arrElement)
	{
		// Get db object
		$dbWriter = $this->getDbWriter();

		// Query for get total records in product table
		$query = "
			SELECT COUNT(*) as total
			FROM {$this->tableName} ";
		// Add where
		$query .= $this->buildWhereClause($arrElement, $dbWriter);

		// Execute query
		$result = $dbWriter->fetchRow($query);
		if( !is_null($result) ) {
			return (int) $result['total'];
		}

		// Return no record
		return 0;
	}
}